<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style>
        .hide {
            display: none;
        }

        .c1 {
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            background: rgba(0, 0, 0, .5);
            z-index: 2;
        }

        .c2 {
            background-color: white;
            position: fixed;
            width: 400px;
            height: 300px;
            top: 50%;
            left: 50%;
            z-index: 3;
            margin-top: -150px;
            margin-left: -200px;
            padding-top: 2%;
            padding-left: 2%;
        }

        .c3 {
            background-color: white;
            position: fixed;
            width: 400px;
            height: 300px;
            top: 50%;
            left: 50%;
            z-index: 3;
            margin-top: -150px;
            margin-left: -200px;
            padding-top: 2%;
            padding-left: 2%;
        }

        #modal p {
            margin-left: 80px;
        }

        input::placeholder {
            color: #D0D0D0;
        }

        .aui-btns {
	display: inline-block;
	padding: 6px 12px;
	margin-bottom: 0;
	font-size: 14px;
	font-weight: 400;
	line-height: 1.42857143;
	text-align: center;
	background-color: #fc0606;
	white-space: nowrap;
	vertical-align: middle;
	-ms-touch-action: manipulation;
	  touch-action: manipulation;
	cursor: pointer;
	-webkit-user-select: none;
	 -moz-user-select: none;
	  -ms-user-select: none;
	      user-select: none;
	border: 1px solid #ecf0f1;
	border-radius: 4px;
}
    </style>
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            返修标签补打
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:60px;">

        <div id="citemList" class="aui-content">
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">序列号</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="选择/扫描 序列号" id="barcode" value=""
                                onclick="getIdSelector();" />
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">蓝牙设备</div>
                        <select id="bluetooth">
                        </select>
                    </div>
                </li>
            </ul>

        </div>

       

    </div>
    <div class=' aui-btn aui-btn-info aui-btn-block' onclick='doSearch()'>查询
    </div>
    <div style="width:100%;margin-top:2%;"></div>
    <div class="aui-btn aui-btn-info aui-btn-block" onclick="bluetoothConnect()">蓝牙连接</div>

    <ul class="ui-selectee" id="tbodyHtml">


    </ul>
    
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/hammer.min.js"></script>
<script type="text/javascript">

  // 获取光标所在组件的id
  function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //初始化
    apiready = function () {
        //获取蓝牙列表
        getBluetoothCombox();

        $("#barcode").focus();
        api.setFocus({
            inputId: 'barcode',
            focus: true,
        });

    };
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }

    //蓝牙连接
    function bluetoothConnect() {
        var address = $("#bluetooth").val();
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙"
            });
            return;
        }
        var modulePrinterCpclXC = api.require('modulePrinterCpclXC');
        modulePrinterCpclXC.connectBluetooth({
            address: address,
        }, function (ret, err) {
            if (ret) {
                // api.hideProgress();
                if (ret.code == "S") {
                    api.toast({
                        msg: ret.message
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }


    //查询蓝牙设备
    function getBluetoothCombox() {
        var modulePrinterCpclXC = api.require('modulePrinterCpclXC');
        modulePrinterCpclXC.searchBluetooth({}, function (ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    var list = ret.list;
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("bluetooth"), html);

                    //取缓存中蓝牙设备打印机
                    var bluetooth = localStorage.getItem('bluetooth');
                    if (bluetooth != null && bluetooth != undefined && bluetooth != "") {
                        $("#bluetooth").val(bluetooth);
                    }

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //产品条码监听回车事件
    $("#barcode").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            doSearch()
        }
    });

    //查询
    function doSearch() {
        var barcode = document.getElementById('barcode').value;

        if (barcode == null || barcode == "") {

            api.toast({
                msg: "请先扫描序列号！",
                duration: 3000
            });
            return
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/repairReprintList";
        var data = {
            values: {
                barcode: barcode,
                appVersion: api.appVersion,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<li class='sortable ui-selectee ui-sortable' >";
                        html += "<ul class='aui-list aui-media-list ui-selectee' >";
                        html += "<li class='aui-list-item  ui-selectee' >";

                        html += "<div class='aui-media-list-item-inner ui-selectee' >";
                        html += "<div class='aui-list-item-inner ui-selectee'  >";
                        html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>序列号</div>";
                        html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 20px;color:black;'>" + (list[i].barcode==null?"":list[i].barcode) + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>工序</div>";
                        html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 20px;color:black;'>" + (list[i].opCode==null?"":list[i].opCode) + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>日期</div>";
                        html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 20px;color:black;'>" + (list[i].newDate==null?"":list[i].newDate) + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>检查员</div>";
                        html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 20px;color:black;'>" + (list[i].checkPerson==null?"":list[i].checkPerson)+ "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>班次</div>";
                        html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 20px;color:black;'>" + (list[i].shiftCode==null?"":list[i].shiftCode) + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee'  style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>缺陷类别</div>";
                        html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 20px;color:black;'>" + (list[i].defectType==null?"":list[i].defectType)+ "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title'>缺陷内容</div>";
                        html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 20px;color:black;'>" + (list[i].defectName==null?"":list[i].defectName) + "</div>";
                        html += "</div>";
                        html += "<div class='aui-list-item-text ui-selectee' style='height: 30px;'>";
                        html += "<div class='aui-list-item-label ui-selectee list-tr-title' style='width: 90%;'>备注</div>";
                        html += "<div class='aui-list-item-inner ui-selectee' style='margin-left: 20px;color:black;'>" +(list[i].remark==null?"":list[i].remark) + "</div>";
                        html += "<div class='aui-list-item-right aui-btns ui-selectee'  style='width: 200px;height: 50px;font-size: 30px;color:white;'" +
                            "onclick='repairReprint(\"" + list[i].sysid + "\");'>补打</div>";
                        html += "</div>";
                        html += "</div>";
                        html += "</div>";
                        html += "</li>";
                        html += "</ul>";
                        html += "</li>";
                    }

                    $api.html($api.byId("tbodyHtml"), html);
                    $("#barcode").val("")
                    //失败
                } else if (myobj.code == "1") {
                    $("#barcode").val("")
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //保存
    function repairReprint(sysid) {

        var address = $("#bluetooth").val();

        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: '请选择打印机!',
                duration: 3000
            });
            return;
        }
        localStorage.setItem('bluetooth', address);

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/repairReprint";
        var data = {
            values: {
                sysid: sysid,
                un: un,
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                console.log(ret);
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == 0) {

                    var json = myobj.json;
                    //调用PDA打印
                    printPDA(json, address);

                } else {
                    api.toast({
                        msg: myobj.msg,
                        duration: 3000
                    });
                }
            }
        });


    }



    //打印
    function printPDA(json, address) {
        var modulePrinterCpclXC = api.require('modulePrinterCpclXC');
        modulePrinterCpclXC.printerXc({
            json: json, //打印数据JSON
            address: address //蓝牙地址
        }, function (ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }


</script>

</html>